package com.codeavatar.catools.helper;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaPermissionHelper {

    private final String TAG = this.getClass().getSimpleName();

    private final int PERMISSION_CODE = 0x666666;

    private IActionCallback actionCallback;

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public void requestPermission(@NonNull Activity activity, @NonNull String[] permissions,IActionCallback callback){
        this.actionCallback = callback;
        ActivityCompat.requestPermissions(
                activity, permissions,PERMISSION_CODE
        );
    }

    public void requestPermission(@NonNull Activity activity, @NonNull String permission,IActionCallback callback){
        this.actionCallback = callback;
        ActivityCompat.requestPermissions(
                activity, new String[]{permission},PERMISSION_CODE
        );
    }
    //授权结果
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        if(PERMISSION_CODE == requestCode && permissions.length > 0){
            //授权
            List<String> grantedList = new ArrayList<>();
            List<String> deniedList = new ArrayList<>();
            if(permissions.length == grantResults.length){
                for (int i=0; i<permissions.length; i++){
                    if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                        grantedList.add(permissions[i]);
                    }else{
                        deniedList.add(permissions[i]);
                    }
                }
                if(null != this.actionCallback){
                    if(deniedList.isEmpty()){
                        this.actionCallback.onGranted();
                    }else{
                        this.actionCallback.onDenied(deniedList,grantedList);
                    }
                }
            }
        }
    }

    //需重写 onRequestPermissionsResult
    public void requestPermission(@NonNull Activity activity, @NonNull String[] permissions, @NonNull int requestCode){
        ActivityCompat.requestPermissions(
            activity, permissions,requestCode
        );
    }

    //需重写 onRequestPermissionsResult
    public void requestPermission(@NonNull Activity activity, @NonNull String permission, @NonNull int requestCode){
        ActivityCompat.requestPermissions(
                activity, new String[]{permission},requestCode
        );
    }

    public void checkPermission(@NonNull Activity activity, @NonNull String permission, ICallback callback){
        if(this.checkPermission(activity,permission)){
            callback.granted();
        }else{
            callback.denied();
        }
    }

    public boolean checkPermission(@NonNull Activity activity, @NonNull String permission){
        return (this.checkSelfPermission(activity,permission) == PackageManager.PERMISSION_GRANTED);
    }

    // The callback method.
    public interface ICallback{
        void granted();
        void denied();
    }

    public interface IActionCallback{
        //授权成功
        void onGranted();
        //授权失败
        void onDenied(List<String> deniedList,List<String> grantedList);
    }

    private static CaPermissionHelper init;
    public static CaPermissionHelper init(){
        if(null == init){
            init = new CaPermissionHelper();
        }
        return init;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private int checkSelfPermission(@NonNull Context context, @NonNull String permission){
        return ActivityCompat.checkSelfPermission(context, permission);
    }

    private CaPermissionHelper(){
        // Private constructor to prevent unwanted construction.
    }
}
